package com.business.action.baidu;

import java.io.File;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.business.bean.system.UserBean;
import com.business.task.BaiduAccessTokenTask;
import com.business.util.HttpUtil;

import net.sf.rose.file.util.FileUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.util.DateFormat;
import net.sf.rose.util.StringUtil;

/**
 * 百度文字识别
 */
@Controller
@RequestMapping("/baidu/txt")
public class IocrAction {

	/**
	 * 身份证识别
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/idCard.do")
	public PageBean idCard(HttpServletRequest request, String path) {
		PageBean page = new PageBean();
		// 身份证识别url
		String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
		// 本地图片路径
		try {
			File file = new File(FileUtils.IMAGE_PATH, path);
			byte[] imgData = FileUtils.toByteArray(file);
			String imgStr = StringUtil.toBase64(imgData);
			// 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
			String params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="
					+ URLEncoder.encode(imgStr, "UTF-8");
			/**
			 * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
			 */
			String accessToken = BaiduAccessTokenTask.getAccessToken();
			String result = HttpUtil.post(idcardIdentificate, accessToken, params);
			Map<String, Object> map = StringUtil.parseJson(result);
			map = (Map<String, Object>) map.get("words_result");
			String address = ((Map<String, Object>) map.get("住址")).get("words").toString();
			String birthday = ((Map<String, Object>) map.get("出生")).get("words").toString();
			String userName = ((Map<String, Object>) map.get("姓名")).get("words").toString();
			String idCard = ((Map<String, Object>) map.get("公民身份号码")).get("words").toString();
			String sex = ((Map<String, Object>) map.get("性别")).get("words").toString();
			// String nation = ((Map<String, Object>)
			// map.get("民族")).get("words").toString();
			UserBean bean = new UserBean();
			bean.setUserName(userName);
			bean.setAddress(address);
			bean.setBirthday(DateFormat.format(birthday, DateFormat.DATE_FORMAT_2));
			bean.setIdCard(idCard);
			bean.setSex(sex.equals("男") ? 1 : 2);
			page.setData(bean);
			System.out.println(result);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMessage(e.getMessage());
		}
		return page;
	}

	/**
	 * 车牌号码识别
	 */
	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping("/licensePlate.do")
	public PageBean licensePlate(HttpServletRequest request, String path) {
		PageBean page = new PageBean();
		// 身份证识别url
		String idcardIdentificate = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
		// 本地图片路径
		try {
			File file = new File(FileUtils.IMAGE_PATH, path);
			byte[] imgData = FileUtils.toByteArray(file);
			String imgStr = StringUtil.toBase64(imgData);
			// 识别身份证正面id_card_side=front;识别身份证背面id_card_side=back;
			String params = URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(imgStr, "UTF-8");
			/**
			 * 线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
			 */
			String accessToken = BaiduAccessTokenTask.getAccessToken();
			String result = HttpUtil.post(idcardIdentificate, accessToken, params);
			Map<String, Object> map = StringUtil.parseJson(result);
			map = (Map<String, Object>) map.get("words_result");
			String color = map.get("color").toString();
			String carNumber = map.get("number").toString();
			map.clear();
			map.put("color", color);
			map.put("carNumber", carNumber);
			page.setData(map);
			System.out.println(result);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMessage(e.getMessage());
		}
		return page;
	}
}
